package com.epro.dx.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import com.epro.dx.R;
import com.epro.dx.activity.coupon.fragment.LandingPageCategoryFragment;
import com.epro.dx.activity.coupon.fragment.LandingPageCategoryFragment.OnFlingStateChangedListener;
import com.epro.dx.activity.coupon.fragment.LandingPageCategoryFragment.OnFragmentCreateViewCalled;
import com.epro.dx.adapter.PandingPageFragmentAdapter;
import com.epro.dx.listenner.OnSortViewItemClickListenner;
import com.epro.dx.widget.PagerSlidingTabStrip;
import com.epro.dx.widget.SortView;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.weipu.common.DXApplication;
import com.weipu.common.DXApplication.TrackerName;
import com.weipu.common.facade.model.CatelogModel;
import com.weipu.common.facade.model.ProductModel;
import com.weipu.common.util.Logger;

public class LandingPageActivity extends BaseFragmentActivity implements
		OnSortViewItemClickListenner ,OnFragmentCreateViewCalled{
	private static final String TAG = "LandingPageActivity";

	private static final String ACTIVE_SKU = "Sku";
	private static final String ACTIVE_CATEGORY = "Gategory";
	private static final String CATEGORY_SIZE = "Gategory_Size";
	private static final String ACTIVE_TYPE = "ActiveType";
	private static final String PRODUCT_LIST = "productlist";

	ArrayList<ProductModel> productlist = null;
	ArrayList<CatelogModel> catelist = null;

	// 视图容器
	private ViewPager categoryViewPager;
	// tab列表
	// private TabPageIndicator indicator;
	private PagerSlidingTabStrip indicator;

	// ViewPager的数据适配器
	private PandingPageFragmentAdapter adapter;

	private String activeType;

	private int categorySize;

	private LinearLayout categorysLayout;

	// private FrameLayout skuLayout;
	private SortView sortLayout;

	private String couponCode;

	/**
	 * 商品列表集合
	 */
	private ArrayList<ProductModel> products = null;

	private List<CatelogModel> cateModels;

	private int densityDpi;

	private boolean isLoadingList = false;

	// private OnSortChangeListenner listenner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		tracker = ((DXApplication)getApplication()).getTracker(TrackerName.APP_TRACKER);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_landing_page);
		densityDpi = getDensityDpi();
		activeType = ACTIVE_CATEGORY;
		intent = getIntent();
		getIntentDate(intent, savedInstanceState);
		intView();
		if(products!=null&&null!=cateModels){
			initData();
		}
		// showLayout();
		
	}

	// public void setOnSortChangeListenner(OnSortChangeListenner listenner){
	// this.listenner=listenner;
	// }

	@Override
	protected void onResume() {
		tracker.setScreenName("CouponLandingPage");
		tracker.send(new HitBuilders.ScreenViewBuilder().setNewSession().build());
		super.onResume();
	}
	
	/**
	 * 初始话控件
	 */
	private void intView() {
		sortLayout = (SortView) findViewById(R.id.sort_layout);
		sortLayout.setPopularityStr("d=Popularity");
		sortLayout.setDefaultSortStyle();
		sortLayout.setOnSortViewItemClickListenner(this);
		// skuLayout=(FrameLayout) findViewById(R.id.sku_layout);

		categorysLayout = (LinearLayout) findViewById(R.id.categorys_layout);
		categoryViewPager = (ViewPager) findViewById(R.id.categorys_view_pager);
		indicator = (PagerSlidingTabStrip) findViewById(R.id.indicator_categorys_tab);
		indicator.setIsBisect(false);//设置标题栏不平分显示
		indicator.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				//该处只对FragmentPagerAdapter适配器试用
				LandingPageCategoryFragment fragment = (LandingPageCategoryFragment) adapter.instantiateItem(categoryViewPager,position);
				String currentSortType=fragment.getCurrentFragmentSortType();
				if(null!=currentSortType&&!"".equals(currentSortType)){
					sortLayout.setStyleBySortType(currentSortType);
				}else{
					sortLayout.setPopularityStr("d=Popularity");
					sortLayout.setDefaultSortStyle();
				}
//				adapter.getItem(arg0);
				sortLayout.setVisibility(View.VISIBLE);
				fragment.mIsScroll2Top = false;
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
			}
		});
	}

	/**
	 * 加载数据
	 */
	// private void initData() {
	// // 设置头部显示信息
	// setTitleContent(R.string.Coupon_Category_toptitle);
	// setLeftBtnContent(true, -1);
	// List<LandingPageCategoryFragment> fragmentList=new
	// ArrayList<LandingPageCategoryFragment>();
	// for(int i=0;i<cateModels.size();i++){
	// LandingPageCategoryFragment
	// fragment=LandingPageCategoryFragment.newInstance(i,cateModels.get(i).getBrowseID(),productlist,couponCode);
	// fragmentList.add(fragment);
	// }
	//
	// adapter = new
	// PandingPageFragmentAdapter(getSupportFragmentManager(),fragmentList,cateModels);
	// categoryViewPager.setAdapter(adapter);
	// //orderViewPager.setOffscreenPageLimit(0);
	// indicator.setViewPager(categoryViewPager);
	// indicator.setTextColor(
	// getResources().getColor(R.color.myorder_tab_text_normal),
	// getResources().getColor(R.color.myorder_tab_text_selected));
	// categoryViewPager.setCurrentItem(0);
	// }

	/**
	 * 加载数据
	 */
	private void initData() {
		// 设置头部显示信息
		setTitleContent(R.string.Coupon_Category_toptitle);
		setLeftBtnContent(true, -1);
		adapter = new PandingPageFragmentAdapter(getSupportFragmentManager(),
				cateModels, productlist, couponCode,onFlingStateChangedListener,
				intent.getStringExtra("CurrencyCode"),intent.getStringExtra("CurrencyCodeStr"));
		categoryViewPager.setAdapter(adapter);
		indicator.setViewPager(categoryViewPager);
		indicator.setTextColor(
				getResources().getColor(R.color.myorder_tab_text_normal),
				getResources().getColor(R.color.myorder_tab_text_selected));
		categoryViewPager.setCurrentItem(0);
	}

	OnFlingStateChangedListener onFlingStateChangedListener = new OnFlingStateChangedListener() {

		@Override
		public void onFlingToTop(boolean isToTop) {
			// TODO Auto-generated method stub
			sortLayout.setVisibility(isToTop?View.GONE:View.VISIBLE);
		}
	};

	private Tracker tracker;

	private Intent intent;
	
	/**
	 * 获取页面携带信息
	 * 
	 * @param intent
	 */
	public void getIntentDate(Intent intent, Bundle saveBundle) {
		if (null != intent) {
			productlist = intent.getParcelableArrayListExtra("productlist");
			catelist = intent.getParcelableArrayListExtra("categorylist");
			couponCode = intent.getStringExtra("CouponCode");
		} else if (null != saveBundle) {
			productlist = saveBundle.getParcelable("productlist");
			catelist = saveBundle.getParcelable("categorylist");
			couponCode = saveBundle.getString("CouponCode");
		}

		if (null != productlist) {
			products = productlist;
		}
		if (null != catelist) {
			cateModels = catelist;
		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putParcelableArrayList("productlist", productlist);
		outState.putParcelableArrayList("categorylist", catelist);
		outState.putString("CouponCode", couponCode);
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onPopulayityClick() {
		// fragmentSortRequest();
		sortRequest();
	}

	@Override
	public void onPriceClick() {
		// fragmentSortRequest();
		sortRequest();

	}

	/**
	 * 调用当前Fragemnt的排序方法
	 */
	private void sortRequest() {
		sortLayout.setSortBtnEnable(false);
		LandingPageCategoryFragment fragment = adapter.getCurrentFragment();
		fragment.sortRequest(sortLayout.getCurrentSortType());
	}

//	private void fragmentSortRequest() {
//		LandingPageCategoryFragment fragment = (LandingPageCategoryFragment) adapter
//				.getItem(categoryViewPager.getCurrentItem());
//		fragment.sortRequest(sortLayout.getCurrentSortType());
//	}

	@Override
	public void onDisCountClick() {
		// fragmentSortRequest();
		sortRequest();
	}

	/**
	 * 设置Sort按妞是否可用
	 * 
	 * @param enable
	 */
	public void setSortEnable(boolean enable) {
		sortLayout.setSortBtnEnable(enable);
	}

	@Override
	public void onCreateViewCalled(String currentSortType,int position) {
		int currentPageIndex=categoryViewPager.getCurrentItem();
		//由于可能会加载前后的fragment所以要过滤
		if(currentPageIndex==position){
			if(null!=currentSortType&&!"".equals(currentSortType)){
				sortLayout.setStyleBySortType(currentSortType);
				Logger.i(TAG, currentSortType);
			}else{
				sortLayout.setPopularityStr("d=Popularity");
				sortLayout.setDefaultSortStyle();
				Logger.i(TAG, "currentSortType null");
			}		
		}
//		
	}
//	float y = 0;
//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
//		// TODO Auto-generated method stub
//		System.out.println(">>>>>进入");
//		switch (event.getAction()) {
//		case MotionEvent.ACTION_DOWN:
//			System.out.println(">>>>>ACTION_DOWN");
//			y = event.getY();
//			break;
//		case MotionEvent.ACTION_MOVE:
//			System.out.println(">>>>>ACTION_MOVE");
//			float moveY = event.getY();
//			if((moveY - y)>0){
//				sortLayout.setVisibility(View.VISIBLE);
//			}else{
//				sortLayout.setVisibility(View.GONE);
//			}
//			y = moveY;
//			break;
//
//		default:
//			break;
//		}
//		return false;
//	}
	

}
